package com.cvs.controller;

import java.io.IOException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.cvs.model.DaysEntity;
import com.cvs.model.EventsEntity;
import com.cvs.model.MandatoryEntity;
import com.cvs.model.UserEntity;

import com.cvs.security.SecurityUtilities;
import com.cvs.services.ChmService;

@Controller
public class AdminController {

	@Autowired
	private ChmService controllerService;

	public ChmService getControllerService() {
		return controllerService;
	}

	public void setControllerService(ChmService controllerService) {
		this.controllerService = controllerService;
	}

	@RequestMapping(value = "/admin", method = RequestMethod.GET)
	public ModelAndView getAdminPage() {

		String message = "admin";
		return new ModelAndView("admin", "messsage", message);
	}

	@RequestMapping(value = "/teams", method = RequestMethod.GET)
	public String getTeamMemberPage(ModelMap model, HttpSession session) {

		List<UserEntity> user = new ArrayList<UserEntity>();
		user = controllerService.getUsers();

		model.addAttribute("userTeam", user);
		return "admin";
	}

	@RequestMapping(value = "/showrequests", method = RequestMethod.GET)
	public String getReqPage(ModelMap model, HttpSession session) {

		List<DaysEntity> days = new ArrayList<DaysEntity>();
		days = controllerService.getUnapprovedHols();

		for (DaysEntity d : days) {
			System.out.println("Date booked: " + d.getDate() + " Day off: "
					+ d.getStartDate());
		}

		model.addAttribute("days", days);
		return "admin";
	}

	@RequestMapping(value = "/teamConfirm", method = RequestMethod.POST)
	public String setTeams(HttpServletRequest req, ModelMap model,
			HttpSession session, HttpServletResponse resp) throws IOException,
			ParseException {

		String group = req.getParameter("newGroup");
		String user = req.getParameter("userName");

		controllerService.updateUserGroup(user, group);

		return "admin";
	}

	@RequestMapping(value = "/request", method = RequestMethod.POST)
	public String createNewUser(HttpServletRequest req, ModelMap model,
			HttpSession session, HttpServletResponse resp) throws IOException,
			ParseException {

		// returns whether holiday has been accepted
		String accepted = req.getParameter("action");
		System.out.println(req.getParameter("jamcup"));
		System.out.println(accepted);
		// returns the id number of the holiday
		String id = req.getParameter("jamcup");
		// converts string returned to long
		long longID = Long.parseLong(id);
		System.out.println(longID);
		// Updates approval
		controllerService.updateAccepted(longID, accepted);
		return "admin";
	}

	@RequestMapping(value = "/allocation", method = RequestMethod.GET)
	public String allocation(HttpServletRequest req, ModelMap model,
			HttpSession session, HttpServletResponse resp) throws IOException,
			ParseException {

		List<UserEntity> users = new ArrayList<UserEntity>();

		users = controllerService.getUsers();

		model.addAttribute("user", users);
		return "admin";
	}

	@RequestMapping(value = "/allocationChange", method = RequestMethod.POST)
	public String allocated(HttpServletRequest req, ModelMap model,
			HttpSession session, HttpServletResponse resp) throws IOException,
			ParseException {

		System.out.println(req.getParameter("newAllocation"));
		System.out.println(req.getParameter("userID"));
		// Request New Allocation as String
		String allo = req.getParameter("newAllocation");
		// convert string to int
		int allocation = Integer.parseInt(allo);
		String username = req.getParameter("userID");

		// Updates approval
		controllerService.updateUserAllocation(username, allocation);
		return "admin";
	}
	
	@RequestMapping(value = "/mandatoryChange", method = RequestMethod.POST)
	public String mandatory(HttpServletRequest req, ModelMap model,
			HttpSession session, HttpServletResponse resp) throws IOException,
			ParseException {
			
		MandatoryEntity mandatory = new MandatoryEntity();
		SecurityUtilities sec = new SecurityUtilities();
		String User = sec.getLoggedInUserName();
		DateFormat formatter = new SimpleDateFormat("yyyy-M-d");
		String error;
		
		mandatory.setDateCreated(new Date());
		mandatory.setCreatedBy(User);
		String endDateInString = req.getParameter("todate");

		try {
			Date parse = formatter.parse(endDateInString.trim());
			mandatory.setMandatoryDay(parse);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		System.out.println("cool");
		if(endDateInString==""){
			error = "no date selected!";
			model.addAttribute("error", error);
			System.out.println("No date selected!");
		}
		else{
			controllerService.createMandatoryRecord(mandatory);
		}
		return "admin";
	}
	@RequestMapping(value = "/eventCreate", method = RequestMethod.POST)
	public String eventCreate(HttpServletRequest req, ModelMap model,
			HttpSession session, HttpServletResponse resp) throws IOException,
			ParseException {
			
		EventsEntity event = new EventsEntity();
		SecurityUtilities sec = new SecurityUtilities();
		String User = sec.getLoggedInUserName();
		DateFormat formatter = new SimpleDateFormat("yyyy-M-d");
		String error;
		
		event.setCreatedBy(User);
		event.setDateCreated(new Date());
		event.setEventInfo(req.getParameter("event"));
		event.setEventTitle(req.getParameter("title"));
		String endDateInString = req.getParameter("todate");

		try {
			Date parse = formatter.parse(endDateInString.trim());
			event.setEventDate(parse);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		System.out.println("cool");
		if(endDateInString==""){
			error = "no date selected!";
			model.addAttribute("error", error);
			System.out.println("No date selected!");
		}
		else{
			System.out.println("New Event");
			controllerService.createEventRecord(event);
		}
		return "admin";
	}
	
	
	@RequestMapping(value = "/mandatory", method = RequestMethod.GET)
	public String mando(HttpServletRequest req, ModelMap model,
			HttpSession session, HttpServletResponse resp) throws IOException,
			ParseException {
		String mandatory ="Set Mandatory Day";
		model.addAttribute("mandatory", mandatory);
		
		return "admin";
	}
	@RequestMapping(value = "/event", method = RequestMethod.GET)
	public String event(HttpServletRequest req, ModelMap model,
			HttpSession session, HttpServletResponse resp) throws IOException,
			ParseException {
		String event ="Create New Event";
		model.addAttribute("event", event);
		
		return "admin";
	}
}
